home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / Ellipse.java < prev    next >
Text File  |  1998-08-21  |  4KB  |  129 lines

  1. package symantec.itools.awt.shape;
  2.  
  3. import java.awt.Graphics;
  4. import java.awt.Color;
  5.  
  6. //  07/30/97    LAB    Updated version to 1.1.  Added contains method.  Modified paint to use the
  7. //                calculated bevel colors if it was in BEVEL_RAISED or BEVEL_LOWERED mode.
  8. //    08/15/97    LAB    Made paint call super.paint to ensure the colors would get updated if needed.
  9.  
  10. /**
  11.  * This class forms the Ellipse shape component.
  12.  * @see symantec.itools.awt.shape.Circle
  13.  * @version 1.1, July 28, 1997
  14.  * @author Symantec
  15.  */
  16. public class Ellipse extends Shape
  17. {
  18.     /**
  19.      * Constructs a default Ellipse.
  20.      */
  21.     public Ellipse()
  22.     {
  23.     }
  24.  
  25.     /**  
  26.      * Checks whether this component "contains" the specified (x, y)
  27.      * location, where x and y are defined to be relative to the 
  28.      * coordinate system of this component.  
  29.      * @param x the x coordinate 
  30.      * @param y the y coordinate
  31.      * @see java.awt.Component#getComponentAt
  32.      */
  33.     public boolean contains(int x, int y)
  34.     {
  35.         //If the coordinate is outside the enclosing rectangle, then it's outside the ellipse.
  36.         if(! super.contains(x, y))
  37.             return false;
  38.    
  39.         double a        = (double) width / 2;
  40.         double b        = (double) height / 2;
  41.         double dx        = (x - a) * ((double) height / width);
  42.         double dy        = (y - b);
  43.  
  44.         if (fill)
  45.         {
  46.             return (dx * dx + dy * dy <= b * b);
  47.         }
  48.         else
  49.         {
  50.             // Our calculations for the ellipse are too precice to exactly match
  51.             // the pixels that are drawn, so we need to fudge it a little.
  52.             return (Math.abs((dx * dx + dy * dy + 30) - (b * b)) < 60);
  53.         }
  54.     }
  55.  
  56.     /**
  57.      * Paints the ellipse using the given graphics context.
  58.      * This is a standard Java AWT method which typically gets called
  59.      * by the AWT to handle painting this component. It paints this component
  60.      * using the given graphics context. The graphics context clipping region
  61.      * is set to the bounding rectangle of this component and its <0,0>
  62.      * coordinate is this component's top-left corner.
  63.      *
  64.      * @param g the graphics context used for painting
  65.      * @see java.awt.Component#repaint
  66.      * @see java.awt.Component#update
  67.      */
  68.     public void paint(Graphics g)
  69.     {
  70.         super.paint(g);
  71.         
  72.         g.clipRect(0, 0, width, height);
  73.  
  74.         int w = width - 1, h = height - 1;
  75.  
  76.         switch (style)
  77.         {
  78.             case BEVEL_LINE :
  79.                  if (fill)
  80.                 {
  81.                     g.setColor(fillColor);
  82.                     g.fillOval(0, 0, width, height);
  83.                 }
  84.                    g.setColor(getForeground());
  85.                 g.drawOval(0, 0, w, h);
  86.                 break;
  87.  
  88.             case BEVEL_LOWERED :
  89.                  if (fill)
  90.                 {
  91.                     g.setColor(fillColor);
  92.                     g.fillOval(0, 0, width, height);
  93.                 }
  94.                 g.setColor(bevelDarkerColor);
  95.                 g.drawArc(0, 0, w, h, 45, 180);
  96.  
  97.                 g.setColor(bevelLighterColor);
  98.                 g.drawArc(0, 0, w, h, 225, 180);
  99.                 break;
  100.  
  101.             case BEVEL_RAISED :
  102.                  if (fill)
  103.                 {
  104.                     g.setColor(fillColor);
  105.                     g.fillOval(0, 0, width, height);
  106.                 }
  107.                 g.setColor(bevelLighterColor);
  108.                 g.drawArc(0, 0, w, h, 45, 180);
  109.  
  110.                 g.setColor(bevelDarkerColor);
  111.                 g.drawArc(0, 0, w, h, 225, 180);
  112.                 break;
  113.                 
  114.             default :
  115.                 if (fill)
  116.                 {
  117.                     g.setColor(fillColor);
  118.                     g.fillOval(0, 0, width, height);
  119.                 }
  120.                 else
  121.                 {
  122.                     g.setColor(getForeground());
  123.                     g.drawOval(0, 0, w, h);
  124.                 }
  125.         }
  126.     }
  127. }
  128.  
  129.